<!DOCTYPE html>
<html lang="zh-cn"><head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/main.css" rel="stylesheet">

    <link rel="shortcut icon" type="image/x-icon" href="/img/favicon.ico" />

    <title>GPIO相关知识 | wiseAI的小站</title>
</head><body><div class="container-fluid">
    <nav class="navbar fixed-top navbar-expand-sm navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">
                <img src="/img/favicon.ico" alt="" width="30" height="24" class="d-inline-block align-text-top">
                wiseAI的小站
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/articles/">文章</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/categories/">分类</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/tags/">关键字</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/post/golang/">golang编程</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/about/">关于</a>
                    </li>
                    
                </ul>
                <form class="d-flex">
                    <input id="search-query" class="form-control me-2" type="search" placeholder="Search for anything..." aria-label="Search">
                </form>
            </div>
        </div>
    </nav>
</div>




<div id="content">
<div class="container article">
	<h1>GPIO相关知识</h1>
	<div class="post-meta">
		<div>
			
			
			By wiseai | 2022-05-30 | 1 minutes
		</div>
		<div class="tags">
			
			<a class="btn btn-light links btn-sm" href="/tags/gpio/">gpio</a>
			
			<a class="btn btn-light links btn-sm" href="/tags/%E6%A0%91%E8%8E%93%E6%B4%BE/">树莓派</a>
			
		</div>
	</div>
	<div>
		<div class="article-post">
			<h2 id="linux查看gpio状态">linux查看gpio状态</h2>
<p><code>cat /sys/kernel/debug/gpio</code></p>
<p>有个blog里有这个方法，去试试：</p>
<pre tabindex="0"><code>mkdir /tmp/debug
mount -t debugfs debugfs /tmp/debug
cat /tmp/debug/gpio
</code></pre><h2 id="gpio操作的一些说明">GPIO操作的一些说明</h2>
<ul>
<li>首先，看看系统中有没有“/sys/class/gpio”这个文件夹。如果没有请在编译内核的时候加入</li>
</ul>
<p>Device Drivers-&gt; GPIO Support -&gt;/sys/class/gpio/… (sysfs interface)</p>
<ul>
<li>/sys/class/gpio 的使用说明：</li>
</ul>
<p>gpio_operation 通过/sys/文件接口操作IO端口 GPIO到文件系统的映射</p>
<p>◇  控制GPIO的目录位于/sys/class/gpio</p>
<p>◇  /sys/class/gpio/export文件用于通知系统需要导出控制的GPIO引脚编号</p>
<p>◇  /sys/class/gpio/unexport 用于通知系统取消导出</p>
<p>◇  /sys/class/gpio/gpiochipX目录保存系统中GPIO寄存器的信息，包括每个寄存器控制引脚的起始编号base，寄存器名称，引脚总数 导出一个引脚的操作步骤</p>
<p>◇  首先计算此引脚编号，引脚编号 = 控制引脚的寄存器基数 + 控制引脚寄存器位数</p>
<p>◇  向/sys/class/gpio/export写入此编号，比如12号引脚，在shell中可以通过以下命令实现，<code>echo 12 &gt; export</code>命令成功后生成/sys/class/gpio/gpio12目录，如果没有出现相应的目录，说明此引脚不可导出</p>
<p>◇  direction文件，定义输入输入方向，可以通过下面命令定义为输出。direction接受的参数：in, out, high, low。high/low同时设置方向为输出，并将value设置为相应的1/0</p>
<p>◇  value文件是端口的数值，为1或0</p>
<h2 id="例子">例子：</h2>
<p>进入/sys/class/gpio/文件夹：</p>
<ol>
<li>将gpio7重定向用户定义设备，生成gpio7目录</li>
</ol>
<p><code># echo 7 &gt; export</code></p>
<ol start="2">
<li>进入gpio7目录并查看文件</li>
</ol>
<p><code># cd gpio7</code></p>
<p><code># ls</code></p>
<p>direction设置引脚方向，输入还是输出</p>
<p>value设置引脚状态，高电平还是低电平</p>
<ol start="3">
<li>输入状态</li>
</ol>
<ul>
<li>设置引脚状态为输入状态</li>
</ul>
<p><code># echo in &gt; direction</code></p>
<ul>
<li>查看引脚高低电平</li>
</ul>
<p><code># cat value </code></p>
<ol start="4">
<li>输出状态</li>
</ol>
<ul>
<li>设置引脚状态为输出状态</li>
</ul>
<p><code># echo out &gt; direction</code></p>
<ul>
<li>设置输出高电平</li>
</ul>
<p><code># echo 1 &gt; value</code></p>
<ul>
<li>设置输出低电平</li>
</ul>
<p><code># echo 0 &gt; value</code></p>
<ol start="5">
<li>注销</li>
</ol>
<p>测试完毕之后返回/sys/class/gpio/目录，并将gpio注销</p>
<p><code># cd /sys/class/gpio/</code></p>
<p><code># echo 7 &gt; /sys/class/gpio/unexport</code></p>
<p>这篇博客很不错：https://zhuanlan.zhihu.com/p/66660750
<a href="https://blog.csdn.net/k1ang/article/details/107117077">https://blog.csdn.net/k1ang/article/details/107117077</a></p>

		</div>
	</div>
</div>


<div class="container">
    
    <div class="row">
        
        <div class="col-5">
            <a class="page-link link-dark text-end dh" href="/post/linux%E5%AE%89%E8%A3%85hp%E6%89%93%E5%8D%B0%E6%9C%BA%E9%A9%B1%E5%8A%A8/">
                <h5>前一篇</h5><br>
                Linux安装hp打印机驱动
            </a>            
        </div>
        
        <div class="col-2">
        </div>
        
        <div class="col-5">
            <a class="page-link link-dark text-start dh" href="/post/golang%E7%9A%84build%E5%91%BD%E4%BB%A4/">
                <h5>后一篇</h5><br>
                golang的build命令编译
            </a>            
        </div>
        
    </div>
    
</div>

        </div><br><br>
<footer class="container">
    <h2>友情链接</h2>
    <hr>
    <nav class="nav nav-pills flex-column flex-sm-row">
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/gnzg/index.html" target="_blank">孤鸟之歌</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/mm/index.html" target="_blank">生成随机字符</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/md/index.html" target="_blank">MarkDown</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://gitee.com/" target="_blank">Gitee</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://github.com/" target="_blank">Github</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.aliyun.com/" target="_blank">阿里云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://cloud.tencent.com/" target="_blank">腾讯云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.oschina.net/" target="_blank">OSCHINA开源中国</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://gitee.com/wiseai/the-way-to-go_ZH_CN/blob/master/eBook/directory.md" target="_blank">the way to go</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://topgoer.com/" target="_blank">golang文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://goframe.org/display/gf" target="_blank">GoFrame</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.aliyundrive.com/" target="_blank">阿里云盘</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://cn.vuejs.org/" target="_blank">vue3文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://element-plus.gitee.io/zh-CN/" target="_blank">element-plus文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.runoob.com/vue3/vue3-tutorial.html" target="_blank">vue3菜鸟教程</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://v5.bootcss.com/" target="_blank">bootstrap v5 中文文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.bootstrap.cn/" target="_blank">bootstrap文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://caddy2.dengxiaolong.com/docs/" target="_blank">caddy中文教程</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.58pic.com/" target="_blank">千图</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://ifonts.com/" target="_blank">iFonts</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://marketing.qiniu.com/" target="_blank">七牛云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.ixigua.com/" target="_blank">西瓜视频</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/yugang/index.html" target="_blank">鱼缸计算</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://justcc.mengkang.net/#/" target="_blank">C语言JustCC</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/pptist/" target="_blank">PPTist</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="/index.xml" target="_blank">RSS</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.kancloud.cn/idcpj/python/418553" target="_blank">参考资料</a>
        
    </nav>

    <div class="copyright text-center">
      <span class="power-by">
        Powered by <a class="links" href="https://gohugo.io" target="_blank">Hugo</a>
    </span>
    <span>|</span>
    <span>
        Theme - <a class="links" href="https://github.com/wiseai-go/blog-hugo" target="_blank">WiseAI</a>
    </span>
    <br>
    <span class="copyright-year">
        &copy;
        
        2017 -
        2023<span>
            陇ICP备15000157号
            
        </span></span>

</div>
</footer>
<script src="/js/bootstrap.bundle.min.js"></script>


</body>
</html>
